package com.example.springcloudfeign.server.impl;

import com.example.springcloudfeign.feign.ServerFeign;
import com.example.springcloudfeign.pojo.StudentPo;
import com.example.springcloudfeign.server.FeignService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.netflix.hystrix.contrib.javanica.conf.HystrixPropertiesManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class FeignServiceImpl implements FeignService {
    @Autowired
    private ServerFeign serverFeign;

    @Override
    public String feignService() {

        //serverFeign.service();
        return serverFeign.service();
    }


    @Override
    public String feignWithParam() {

        //serverFeign.service();

        return serverFeign.feginRequestBody("廖钒", "18");
    }

    @Override
    public String feignWithRestful() {
        return serverFeign.feignWithRestful("Evangelion", 123);
    }

    @HystrixCommand(fallbackMethod = "myfallback", commandProperties = {
            @HystrixProperty(name = HystrixPropertiesManager.CIRCUIT_BREAKER_REQUEST_VOLUME_THRESHOLD, value = "10"),
            @HystrixProperty(name = HystrixPropertiesManager.EXECUTION_ISOLATION_THREAD_TIMEOUT_IN_MILLISECONDS, value = "20"),
            @HystrixProperty(name = HystrixPropertiesManager.CIRCUIT_BREAKER_ERROR_THRESHOLD_PERCENTAGE, value = "30")
    })
    @Override
    public StudentPo feignWithRequestBody() {
        StudentPo studentPo = new StudentPo();
        studentPo.setId(1);
        studentPo.setName("liaofan");
        return serverFeign.feignWithRequestBody(studentPo);
    }


    int i = 0;

    public StudentPo myfallback() {
        i++;
        System.out.println("服务降级,降级了" + i + "次!");
        return null;
    }


    @Override
    public StudentPo feignWithNormalFormData() {
        StudentPo studentPo = new StudentPo();
        studentPo.setId(1);
        studentPo.setName("liaofan");
        return serverFeign.feignWithNormalFormDataDemo6(studentPo);
    }

//    @HystrixCollapser(batchMethod = "mybatch", scope = com.netflix.hystrix.HystrixCollapser.Scope.GLOBAL,
//            collapserProperties = {
//                    @HystrixProperty(name = "timerDelayInMillisesecond", value = "10"),
//                    @HystrixProperty(name = "maxReqeustInBatch", value = "200")
//            }
//
//    )
//    @Override
//    public Future<String> hystrix_Collasper(String name) {
//        System.out.println("请求合并的方法恒不会会执行");
//        serverFeign.feignWithHystrixCollasper("廖钒");
//        return null;
//    }



}
